﻿Imports Library

Public Class frmSetMyStocks

    Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
        Dim i As Integer
        Dim MyStockTmp As clsMyStockInfo

        g_MyStocks.Clear()

        For i = 0 To dgvMyStock.RowCount - 1
            MyStockTmp = New clsMyStockInfo
            MyStockTmp.StockID = dgvMyStock.Rows(i).Cells(0).Value.ToString
            MyStockTmp.NumberOfShares = dgvMyStock.Rows(i).Cells(1).Value.ToString
            MyStockTmp.CostPrice = dgvMyStock.Rows(i).Cells(2).Value.ToString
            MyStockTmp.DateBuy = dgvMyStock.Rows(i).Cells(3).Value.ToString

            g_MyStocks.Add(MyStockTmp)
        Next

        If SaveMyStockToFile(g_MyStocks) Then
            MsgBox("保存成功")
        End If

    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEdit.Click
        dgvMyStock_SelectionChanged(Nothing, Nothing)

        groupEdit.Visible = Not groupEdit.Visible

    End Sub

    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnLoad_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoad.Click
        LoadDataFromDB()
    End Sub

    ''' <summary>
    ''' add current data from controls to gridview
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        If CheckRecordData() Then
            Dim NewData As Object() = {txtStockID.Text, CInt(txtStockCount.Text), CDbl(txtcostPrice.Text), dtpBuyDate.Value}
            dgvMyStock.Rows.Add(NewData)
        End If

    End Sub

    ''' <summary>
    ''' delete selected row from gridview
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        If dgvMyStock.SelectedRows.Count < 1 Then
            Exit Sub
        End If

        dgvMyStock.Rows.Remove(dgvMyStock.SelectedRows(0))
    End Sub

    ''' <summary>
    ''' save record from controls to gridview
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnSaveRecord_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveRecord.Click
        If CheckRecordData() = False Then Exit Sub
        If dgvMyStock.SelectedRows.Count < 1 Then Exit Sub

        Dim NewData As Object() = {txtStockID.Text, CInt(txtStockCount.Text), CDbl(txtcostPrice.Text), dtpBuyDate.Value}
        dgvMyStock.SelectedRows(0).SetValues(NewData)


    End Sub



    Private Function CheckRecordData() As Boolean
        'check input
        If txtStockID.Text.Length <> 6 Then
            txtStockID.SelectAll()
            txtStockID.Focus()
            MsgBox("股票代码请输入6位")
            Return False
        End If

        If IsNumeric(txtStockCount.Text) = False Then
            txtStockCount.SelectAll()
            txtStockCount.Focus()
            MsgBox("股票数量请输入数字")
            Return False
        End If
        If (CInt(txtStockCount.Text)) < 0 Or (CDbl(txtStockCount.Text)) <> Fix(CDbl(txtStockCount.Text)) Then
            txtStockCount.SelectAll()
            txtStockCount.Focus()
            MsgBox("股票数量请输入正整数")
            Return False
        End If

        If IsNumeric(txtcostPrice.Text) = False Then
            txtcostPrice.SelectAll()
            txtcostPrice.Focus()
            MsgBox("股票价格请输入数字")
            Return False
        End If
        If CInt(txtcostPrice.Text) < 0 Then
            txtcostPrice.SelectAll()
            txtcostPrice.Focus()
            MsgBox("股票数量请输入正数")
            Return False
        End If

        Return True
    End Function


    Private Sub frmSetMyStocks_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        LoadDataFromDB()
    End Sub

    Private Sub dgvMyStock_SelectionChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dgvMyStock.SelectionChanged
        Try
            txtStockID.Text = dgvMyStock.SelectedRows(0).Cells(0).Value
            txtStockCount.Text = dgvMyStock.SelectedRows(0).Cells(1).Value
            txtcostPrice.Text = dgvMyStock.SelectedRows(0).Cells(2).Value
            dtpBuyDate.Value = CDate(dgvMyStock.SelectedRows(0).Cells(3).Value)
        Catch ex As Exception
            Exit Sub
        End Try
        
    End Sub

    Private Sub LoadDataFromDB()
        dgvMyStock.Rows.Clear()

        g_MyStocks = LoadMyStockFromFile()
        If g_MyStocks.Count = 0 Then Exit Sub

        dgvMyStock.Rows.Add(g_MyStocks.Count)
        For i As Integer = 0 To g_MyStocks.Count - 1
            dgvMyStock.Rows(i).Cells(0).Value = g_MyStocks(i).StockID
            dgvMyStock.Rows(i).Cells(1).Value = g_MyStocks(i).NumberOfShares
            dgvMyStock.Rows(i).Cells(2).Value = g_MyStocks(i).CostPrice
            dgvMyStock.Rows(i).Cells(3).Value = g_MyStocks(i).DateBuy
        Next
    End Sub
End Class